interface CacheItem {
    val: any;
    timeStamp?: number;
}
interface CacheItemObj {
    key: string;
    val: CacheItem | any;
}
const CACHE_KEY = 'cacheArray';
type Unit_DTYPE = 'mm' | 'ss'

function calExpTime(time: number, unit: Unit_DTYPE = 'mm'): number {
  const curTime = new Date().getTime();
  let expTime = 0;
  if (unit === 'ss') {
    expTime = curTime + time * 1000;
  } else if (unit ==='mm') {
    expTime = curTime + time * 60 * 1000;
  }
  return expTime;
}
function isNotExpired(cache: CacheItem): boolean {
    return !!cache && (cache.timeStamp === undefined || cache.timeStamp > Date.now());
}
const getCacheItems = () => {
    return JSON.parse(localStorage.getItem(CACHE_KEY) || '[]');
};
const saveCacheItems = (items:CacheItemObj): void => {
    localStorage.setItem(CACHE_KEY, JSON.stringify(items));
};

function setCache (key: string, val: any, time?: number, unit?: Unit_DTYPE){
    if(time){
       const timeStamp = calExpTime(time, unit);
       setCacheLoca(key, {val, timeStamp})
    } else {
        setCacheLoca(key, val)
    }
}

function getCache (key: string){
    const cache =  getCacheLoca(key)
    // 判断是否过期
    if(isNotExpired(cache)){
        return cache.val;
    } else {
        delCache(key);
    }
    return null;
}
function setCacheLoca(key: string, val: any) {
    let cacheArray = getCacheItems();
    const existingIndex = cacheArray.findIndex((item:CacheItemObj) => item.key === key);
    const obj = val instanceof Object 
        ? { key, val } 
        : { key, val: { val } };
    if (existingIndex >= 0) {
        cacheArray[existingIndex] = obj;
    } else {
        cacheArray.unshift(obj);
    }

    saveCacheItems(cacheArray);
}
function getCacheLoca(key: string) {
    const cacheArray = getCacheItems();
    const cacheItem = cacheArray.find((item:CacheItemObj) => item.key === key);
    return cacheItem ? cacheItem.val : null;
}
function delCache(key: string) {
    const cacheArray = getCacheItems();
    const updatedCacheArray = cacheArray.filter((item:CacheItemObj) => item.key !== key);
    saveCacheItems(updatedCacheArray);
}
function clearCache() {
    localStorage.removeItem(CACHE_KEY);
}
function comCache (key: string){
    const cache =  getCacheLoca(key);
    // 判断是否过期
    if(isNotExpired(cache)){
        let time = new Date(cache.timeStamp).getTime() - new Date().getTime();
        let seconds = Math.floor(time / 1000);
        return seconds;
    } else {    
        delCache(key);
    }
    return null;
}

function setCookie (cname: string, cvalue: string, exdays: number){
    var curTime = new Date();
	curTime.setTime(curTime.getTime() + (exdays*24*60*60*1000));
	var expires = "expires=" + curTime.toUTCString();
	document.cookie = cname + "=" + cvalue+"; " + expires;
}
function getCookie (cname: string){
    var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i].trim();
		if (c.indexOf(name)==0) { 
            return c.substring(name.length,c.length); 
        }
	}
	return "";
}
function delCookie (name: string){
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

export { clearCache, delCookie, comCache, delCache, getCache, getCacheLoca, getCookie,  setCache, setCacheLoca, setCookie };